package cn.wuxing.mediation.businessplatform.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.wuxing.mediation.businessplatform.domain.annotation.BusinessTag;
import cn.wuxing.mediation.businessplatform.domain.constants.BusinessListConstants;
import cn.wuxing.mediation.businessplatform.domain.dto.NotDisputeEventListDTO;
import cn.wuxing.mediation.businessplatform.domain.enums.EventCategoryEnum;
import cn.wuxing.mediation.businessplatform.domain.enums.EventStateEnum;
import cn.wuxing.mediation.businessplatform.domain.enums.OperatingTypeEnum;
import cn.wuxing.mediation.businessplatform.domain.vo.ChangeWindowDetailVO;
import cn.wuxing.mediation.businessplatform.domain.vo.NotDisputeEventLisVO;
import cn.wuxing.mediation.businessplatform.factory.BusinessFactory;
import cn.wuxing.mediation.businessplatform.mapper.business.CmEventApplicantMapper;
import cn.wuxing.mediation.businessplatform.mapper.business.CmOperatingRecordMapper;
import cn.wuxing.mediation.businessplatform.mapper.system.DeptMapper;
import cn.wuxing.mediation.businessplatform.service.BaseBusinessListService;
import cn.wuxing.mediation.common.core.domain.entity.CmEventApplicant;
import cn.wuxing.mediation.common.core.domain.entity.CmOperatingRecord;
import cn.wuxing.mediation.common.core.domain.entity.SysDept;
import cn.wuxing.mediation.common.utils.StringUtils;
import cn.wuxing.mediation.common.utils.bean.BeanUtils;
import cn.wuxing.mediation.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: <描述>
 * @date: 2024/9/15 21:07
 * @author: yang
 * @version: 1.0
 */
@Service
@BusinessTag(tag = BusinessListConstants.NOT_DISPUTE_EVENT)
public class NotDisputeEventListServiceImpl extends
        BaseBusinessListServiceImpl<NotDisputeEventLisVO, NotDisputeEventListDTO> {

    @Autowired
    private CmEventApplicantMapper cmEventApplicantMapper;

    @Override
    public ChangeWindowDetailVO changeWindowDetail(Long eventId) {
        return getChangeWindowDetail(eventId, EventCategoryEnum.NOT_DISPUTE_EVENT.getCode());
    }

    @Override
    public Map<String, Integer> getBusinessLgetApplicantHistoryNum(String idCard) {

        Map<String, Integer> map = new HashMap<>(1);
        if (StringUtils.isBlank(idCard)) {
            map.put("num", 0);
            return map;
        }

        Integer num = this.cmEventApplicantMapper.selectCount(
                Wrappers.<CmEventApplicant>lambdaQuery().eq(CmEventApplicant::getIdCard, idCard)
                        .eq(CmEventApplicant::getEventCategory, EventCategoryEnum.NOT_DISPUTE_EVENT));

        map.put("num", num == null ? 0 : num);
        return map;
    }

    @Override
    public void importTemplate(HttpServletRequest request, HttpServletResponse response) {
        List<NotDisputeEventLisVO> list = BusinessFactory.doInstanceRealization(BusinessListConstants.NOT_DISPUTE_EVENT,
                        BaseBusinessListService.class)
                .getBusinessList(request);
        ExcelUtil<NotDisputeEventLisVO> util = new ExcelUtil<NotDisputeEventLisVO>(NotDisputeEventLisVO.class);
        util.exportExcel(response, list, "非纠纷事项");
    }

    @Override
    void handlerList(List<NotDisputeEventLisVO> list) {
        if (CollUtil.isEmpty(list)) {
            return;
        }

        list.forEach(item -> {
            item.setEventStatus(EventStateEnum.getDesc(item.getEventState()));
        });

    }

    @Override
    void handerParam(NotDisputeEventListDTO dto) {
        if (StringUtils.isNotBlank(dto.getPageType())) {
            switch (dto.getPageType()) {
                // 列表类型 1.待受理 2.已受理 3.待调解 4.已调解
                case "1":
                    dto.setEventStates(Collections.singletonList(EventStateEnum.WAIT_ACCEPT.getCode()));
                    break;

                case "2":
                    dto.setEventStates(
                            Arrays.asList(EventStateEnum.WAIT_CLOSE.getCode(), EventStateEnum.CLOSE.getCode()));
                    break;
            }
        }
    }
}
